package com.yxc.elasticsearchdemo;

import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.UpdateByQueryRequest;
import org.elasticsearch.script.Script;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

/**
 * @author: 杨旭晨
 * @time: 2022/6/22 19:21
 **/
@Slf4j
@SpringBootTest
public class UpdateByQuery {

    @Autowired
    private RestHighLevelClient client;

    @Test
    void updateByQuery() throws IOException {
        long startTime = 0L;
        long endTime = 0L;
        // 更新不同数据量的数据
        for (Integer dataSize : Constant.dataSizeList) {
            startTime = System.currentTimeMillis();
            UpdateByQueryRequest updateByQueryRequest = new UpdateByQueryRequest(Constant.esIndex);
            updateByQueryRequest.setQuery(QueryBuilders.matchQuery("cName", "更新测试" + dataSize));
            updateByQueryRequest.setScript(new Script("ctx._source['cValue']='使用updateByQuery更新" + dataSize + "'"));
            endTime = System.currentTimeMillis();
            log.info("使用updateById更新，数据量{}，使用时间：{}", dataSize, endTime - startTime);
        }
    }

}
